package com.swsc.xapp.utils;

import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class OfflineChangeUtils {
    public static void clear(String fileName){
        NativeJSUtils.writeString2File(fileName, "");
    }
    public static List<Integer> readDefectChange(){
        String context = NativeJSUtils.readFile2String(ConstantUils.FILE_OFFLINE_DEFECT_CHANGE);
        return JSONArray.parseArray(context, Integer.class);
    }
    public static void saveDefectChange(Long defectId){
        String context = NativeJSUtils.readFile2String(ConstantUils.FILE_OFFLINE_DEFECT_CHANGE);
        List<Long> defectIdList = null;
        if(null == context){
            defectIdList = new ArrayList<>();
            defectIdList.add(defectId);
        }else {
            defectIdList = JSONArray.parseArray(context, Long.class);
            if(null == defectIdList){
                defectIdList = new ArrayList<>();
            }
            if(!defectIdList.contains(defectId)){
                defectIdList.add(defectId);
            }
        }
        NativeJSUtils.writeString2File(ConstantUils.FILE_OFFLINE_DEFECT_CHANGE, JSONArray.toJSONString(defectIdList));
    }
    public static List<Integer> readDefectNew(){
        String context = NativeJSUtils.readFile2String(ConstantUils.FILE_OFFLINE_DEFECT_NEW);
        return JSONArray.parseArray(context, Integer.class);
    }
    public static void saveDefectNew(Integer defectId){
        String context = NativeJSUtils.readFile2String(ConstantUils.FILE_OFFLINE_DEFECT_NEW);
        List<Integer> defectIdList = null;
        if(null == context){
            defectIdList = new ArrayList<>();
            defectIdList.add(defectId);
        }else {
            defectIdList = JSONArray.parseArray(context, Integer.class);
            if(null == defectIdList){
                defectIdList = new ArrayList<>();
            }
            if(!defectIdList.contains(defectId)){
                defectIdList.add(defectId);
            }
        }
        NativeJSUtils.writeString2File(ConstantUils.FILE_OFFLINE_DEFECT_NEW, JSONArray.toJSONString(defectIdList));
    }
}
